package de.carcophan.jimdb.gwt.client.view;

import java.util.List;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

import de.carcophan.jimdb.gwt.client.presenter.SearchPresenter;
import de.carcophan.jimdb.gwt.client.presenter.SearchPresenterImpl.Display;
import de.carcophan.jimdb.gwt.shared.entites.Tag;
import de.carcophan.jimdb.gwt.shared.entites.UserMovies;

public class SearchWidgetImpl extends Composite implements Display {

	private static SearchWidgetUiBinder uiBinder = GWT
			.create(SearchWidgetUiBinder.class);

	interface SearchWidgetUiBinder extends UiBinder<Widget, SearchWidgetImpl> {
	}


	@UiField
	Button button;
	@UiField
	TextBox textBox;
	@UiField
	MovieTagDialogImpl movieTagDialogImpl;
	@UiField
	TabLayoutPanel tabPanel;

	@UiField
	MovieGridViewImpl movieGrid;
	UserMovies selectedMovie;

	CellList<UserMovies> cellList;

	@UiField
	ScrollPanel scrollPanel;

	@Override
	public CellList<UserMovies> getCellList() {
		return cellList;
	}

	private SearchPresenter presenter;

	public SearchWidgetImpl() {
		
		initWidget(uiBinder.createAndBindUi(this));
		
		button.setText("Search");
		cellList = new CellList<UserMovies>(new MovieCell());

		final SingleSelectionModel<UserMovies> selectionModel = new SingleSelectionModel<UserMovies>();
		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
					@Override
					public void onSelectionChange(SelectionChangeEvent event) {
						selectedMovie = selectionModel.getSelectedObject();
						presenter.onSelect();
					}
				});

		cellList.setSelectionModel(selectionModel);
		scrollPanel.setHeight("100%");
	}

	@UiHandler("button")
	void onClick(ClickEvent e) {
		if (textBox.getValue() != null && !textBox.getValue().trim().isEmpty()) {
			presenter.onSubmit();
		}
	}

	@UiHandler("textBox")
	void onKeyUp(KeyUpEvent e) {
		if (textBox.getValue() != null && !textBox.getValue().trim().isEmpty()) {
			presenter.onSubmit();
		}
	}

	@UiHandler("tabPanel")
	void onTabSelect(SelectionEvent<Integer> handler) {
		Integer selectedItem = handler.getSelectedItem();
		Widget tabWidget = tabPanel.getTabWidget(selectedItem);
		if (selectedItem == 1) {
			presenter.tabSelected(tabWidget);
		}
	}

	public void setText(String text) {
		button.setText(text);
	}

	public String getText() {
		return button.getText();
	}

	@Override
	public void setPresenter(SearchPresenter presenter) {
		this.presenter = presenter;
		this.movieTagDialogImpl.setPresenter(presenter);
	}

	class MovieCell extends AbstractCell<UserMovies> {

		private static final int COLSPAN_ATTR = 1;

		@Override
		public void render(com.google.gwt.cell.client.Cell.Context context,
				UserMovies userMovies, SafeHtmlBuilder sb) {
			if (userMovies.getTag() != null) {
				String hexColor = userMovies.getTag().getHexColor();
				if (hexColor == null) {
					hexColor = "000000";
				}
				String fontColor = "<strong><font color='#" + hexColor + "'>";
				String tag = userMovies.getTag() == null ? "" : userMovies
						.getTag().getName();
				String tagDisplay = fontColor + tag + "</font></strong>";
				sb.appendHtmlConstant(tagDisplay);
			} else {
				sb.appendHtmlConstant("UNKNOWN");
			}
			sb.appendHtmlConstant("<table>");
			sb.appendHtmlConstant("<tr>");
			addCell(sb, "<img height=150 src='"
					+ userMovies.getMovie().getPoster() + "' />", 8, 1, true);

			sb.appendHtmlConstant("<td>");
			sb.appendHtmlConstant("<tr>");
			addCell(sb,
					"<strong>" + userMovies.getMovie().getTitle()
							+ "</strong> ("
							+ String.valueOf(userMovies.getMovie().getYear())
							+ ")", 1, 2, false);
			sb.appendHtmlConstant("</tr>");
			sb.appendHtmlConstant("<tr>");
			addCell(sb, "Rating: ", 1, 1, false);
			addCell(sb, String.valueOf(userMovies.getMovie().getRating()), 1,
					COLSPAN_ATTR, true);
			sb.appendHtmlConstant("</tr>");
			sb.appendHtmlConstant("<tr>");
			addCell(sb, "Rating count: ", 1, 1, false);
			addCell(sb, String.valueOf(userMovies.getMovie().getRatingCount()),
					1, COLSPAN_ATTR, true);
			sb.appendHtmlConstant("</tr>");
			sb.appendHtmlConstant("<tr>");
			addCell(sb, "Runtime: ", 1, 1, false);
			addCell(sb, userMovies.getMovie().getRuntime(), 1, COLSPAN_ATTR,
					true);
			sb.appendHtmlConstant("</tr>");
			sb.appendHtmlConstant("<tr>");
			addCell(sb, "Genres: ", 1, 1, false);
			addCell(sb, userMovies.getMovie().getGenres(), 1, COLSPAN_ATTR,
					true);
			sb.appendHtmlConstant("</tr>");
			sb.appendHtmlConstant("<tr>");
			addCell(sb, "Rated: ", 1, 1, false);
			addCell(sb, userMovies.getMovie().getRated() == null ? ""
					: userMovies.getMovie().getRated(), 1, COLSPAN_ATTR, true);
			sb.appendHtmlConstant("</tr>");
			sb.appendHtmlConstant("<tr>");
			addCell(sb, "Actors: ", 1, 1, false);
			addCell(sb, userMovies.getMovie().getActors(), 1, COLSPAN_ATTR,
					true);
			sb.appendHtmlConstant("</tr>");
			sb.appendHtmlConstant("<td>");
			sb.appendHtmlConstant("</tr>");

			sb.appendHtmlConstant("</table>");
		}

		private void addCell(SafeHtmlBuilder sb, String content, int rowSpan,
				int colSpan, boolean wrap) {
			sb.appendHtmlConstant("<td rowspan=" + rowSpan + " colspan="
					+ colSpan + " style='"
					+ (!wrap ? "white-space: nowrap" : "") + "' >");
			sb.appendHtmlConstant(content);
			sb.appendHtmlConstant("</td>");
		}

	}

	@Override
	public UserMovies getSelectedMovie() {
		return selectedMovie;
	}

	@Override
	public void setTags(List<Tag> tags) {
		movieTagDialogImpl.setTags(tags);
	}

	@Override
	public String getSelectedTag() {
		return movieTagDialogImpl.getSelectedTag();
	}

	@Override
	public String getTextBoxValue() {
		return movieTagDialogImpl.getTextBoxValue();
	}

	@Override
	public void setDeleteTagImage(ImageResource deleteTagImage) {
		movieTagDialogImpl.setDeleteTagImage(deleteTagImage);
	}

	@Override
	public void setTagForSelectedMovie(Tag tag) {
		movieTagDialogImpl.setSelectedTag(tag);
	}

	@Override
	public void refreshScrollPanel() {
		scrollPanel.remove(cellList);
		scrollPanel.add(cellList);
	}

	@Override
	public String getSearchValue() {
		return this.textBox.getText();
	}

	@Override
	public MovieGridViewImpl getMovieGrid() {
		return movieGrid;
	}

	

}
